<template>
  <div>
    <el-card class="course-list">
      <div slot="header" class="clearfix header-style">
        <el-form ref="form" :inline="true" :model="search">
          <el-form-item label="课程名称">
            <el-input
              placeholder="课程名称"
              v-model="search.courseName"
            ></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="search.status" placeholder="请选择">
              <el-option
                v-for="item in statusSelect"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button class="search-btn" :disabled="loading" @click="onSearch"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="$router.push({ name: 'course-create' })"
          >添加课程</el-button
        >
      </div>
      <el-table v-loading="loading" :data="courseList" style="width: 100%">
        <el-table-column prop="id" label="ID" width="180" align="center">
        </el-table-column>
        <el-table-column
          prop="courseName"
          label="课程名称"
          width="180"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="price" label="价格" align="center">
        </el-table-column>
        <el-table-column prop="sortNum" label="排序" align="center">
        </el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              :disabled="scope.row.statusChangeLoading"
              @change="changeCourseState(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="
                $router.push({
                  name: 'course-update',
                  params: {
                    courseId: scope.row.id,
                  },
                })
              "
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="
                $router.push({
                  name: 'course-section',
                  params: {
                    courseId: scope.row.id,
                  },
                })
              "
              >内容管理</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :disabled="loading"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="search.current"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="search.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getQueryCourses, changeState } from '@/apis/course'

interface CourseListType {
  id: number;
  courseDescription: string;
  courseName: string;
  price: number;
  sortNum: string;
  status: number;
  statusChangeLoading: boolean;
}
export default Vue.extend({
  name: 'course-list',
  data() {
    return {
      search: {
        currentPage: 1,
        pageSize: 10,
        courseName: '',
        status: null
      },
      courseList: [] as CourseListType[],
      pagination: {
        total: 0
      },
      loading: false,
      statusSelect: [
        {
          value: null,
          label: '全部'
        },
        {
          value: 1,
          label: '上架'
        },
        {
          value: 0,
          label: '下架'
        }
      ]
    }
  },
  created() {
    this.loadCourseList()
  },
  methods: {
    async loadCourseList() {
      this.loading = true
      const { data } = await getQueryCourses(this.search)
      if (data.code === '000000') {
        this.courseList = data.data.records.map((item: any) => ({
          id: item.id,
          courseDescription: item.courseDescription,
          courseName: item.courseName,
          price: item.price,
          sortNum: item.sortNum,
          status: item.status,
          statusChangeLoading: false
        }))
        this.pagination.total = data.data.total
      }
      this.loading = false
    },
    onSearch() {
      this.search.currentPage = 1
      this.loadCourseList()
    },
    async changeCourseState(row: any) {
      row.statusChangeLoading = true
      const { data } = await changeState({
        courseId: row.id,
        status: row.status
      })
      if (data.code === '000000') {
        this.$message.success(`${row.status === 0 ? '下架' : '上架'}成功！`)
      }
      row.statusChangeLoading = false
    },
    handleSizeChange(val: number) {
      this.search.pageSize = val
      this.search.currentPage = 1
      this.loadCourseList()
    },
    handleCurrentChange(val: number) {
      this.search.currentPage = val
      this.loadCourseList()
    }
  }
})
</script>

<style lang="scss" scoped>
.course-list {
  .header-style {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .search-btn {
    margin-left: 20px;
  }
  .el-pagination {
    padding-top: 20px;
  }
  .status-wrap {
    display: flex;
    justify-content: center;
    .icon {
      width: 10px;
      height: 10px;
      background-color: red;
      border-radius: 50%;
    }
    .user-valid {
      background-color: green;
    }
  }
}
</style>
